1 package com.inigoserrano.isvalidator.data;
2
3 import java.util.Enumeration;
4 import java.util.Vector;
5
6 import com.inigoserrano.isvalidator.check.InternalCheckException;
7 import com.inigoserrano.isvalidator.check.SimpleCheck;
8 import com.inigoserrano.isvalidator.checkExceptions.CheckException;
9 import com.inigoserrano.isvalidator.errorDo.ErrorDo;
10 import com.inigoserrano.isvalidator.errorDo.ErrorDoGroup;
11 import com.inigoserrano.isvalidator.okDo.OkDo;
12 import com.inigoserrano.isvalidator.okDo.OkDoInternalException;
13
14 /***
15 * This class is the base of all the Data, it isn´t specialized in any domain.
16 * The rest of the Data usually overrides the methods that need and uses this
17 * class´s
18 * @license@
19 * @version @version@
20 * @author @author@
21 **/
22 public class SimpleData implements DataInternal {
23 /***
24 * the store of all the Checks
25 */
26 protected Vector almacen = null;
27 /***
28 * the data to check
29 */
30 protected String valueToCheck = null;
31 /***
32 * if we must throw an exception, this flag is at container level
33 */
34 protected boolean throwException = false;
35 /***
36 * the group of errorDo that listen the parameters
37 */
38 protected ErrorDoGroup constraintListenerContainer = null;
39 /***
40 * One errorDo that listen the parameters
41 */
42 protected ErrorDo constraintListener = null;
43 /***
44 * The OkDo to process this data
45 */
46 protected OkDo validConstraintProcesor = null;
47 /***
48 * if the data match all the constraints
49 */
50 protected boolean match = false;
51 /***
52 * if the data has been checked
53 */
54 protected boolean hasBeenChecked = false;
55 /***
56 * the name of the data (parameterName, argumentName,...)
57 */
58 protected String valueToCheckName = null;
59
60 /***
61 * This is the most simple constructor, only with the data to check
62 * @param valueToCheck the data that has to be checked
63 **/
64 public SimpleData(String valueToCheck) {
65 super();
66 this.almacen = new Vector();
67 this.valueToCheck = valueToCheck;
68 }
69
70 /***
71 * Constructor, with the data and its name
72 * @param valueToCheck the data to check
73 * @param valueToCheckName the name of the data (for example the name of
74 * the parameter of the servlet or the argument of the commandLine)
75 **/
76 public SimpleData(String valueToCheck, String valueToCheckName) {
77 super();
78 this.almacen = new Vector();
79 this.valueToCheck = valueToCheck;
80 this.valueToCheckName = valueToCheckName;
81 }
82
83 /***
84 * Constructor, with the data, its name and if throw an exception
85 * @param valueToCheck the data to check
86 * @param valueToCheckName the name of the data (for example the name of
87 * the parameter of the servlet or the argument of the commandLine)
88 * @param throwException true if must throw an exception when the data
89 * dosn´t fit the constraints
90 **/
91 public SimpleData(
92 String valueToCheck,
93 String valueToCheckName,
94 boolean throwException) {
95 super();
96 this.almacen = new Vector();
97 this.valueToCheck = valueToCheck;
98 this.valueToCheckName = valueToCheckName;
99 this.throwException = throwException;
100 }
101
102 /***
103 * Constructor that indicates the dat to check and explicity if throw
104 * exception if dosen´t match
105 * @param valueToCheck the data to check
106 * @param throwException true if throw an exception when the data
107 * dosen´t match the constraints
108 **/
109 public SimpleData(String valueToCheck, boolean throwException) {
110 super();
111 this.almacen = new Vector();
112 this.valueToCheck = valueToCheck;
113 this.throwException = throwException;
114 }
115
116 /***
117 * Add one constraint to the store, it also notifies to the constraint
118 * that this object is its container.
119 * @param constraint the constraint to add
120 * @throws InternalCheckException for internal errors
121 **/
122 public void addCheck(SimpleCheck constraint)
123 throws InternalCheckException {
124 almacen.addElement(constraint);
125 constraint.addContainer(this);
126 }
127
128 /***
129 * This method add the constraintContainer´s parameters when the data is
130 * invalid. It adds the valueToCheckName if it exist
131 * @param listener the object that receives the parameters
132 **/
133 public void addDataParameters(ErrorDo listener) {
134 //add the name of the value to check if it know it
135 if (this.valueToCheckName != null) {
136 listener.addParameter("valueToCheckName", this.valueToCheckName);
137 }
138 }
139
140 /***
141 * This method gets all the constraints and check if the data match all
142 * this constraints. If all match the return
143 * true, else return false
144 * @return true if the data match all the constraints, else false
145 * @throws CheckException Throws if the data dosn´t match and the flag
146 * is on
147 * @throws InternalCheckException for internal error only
148 **/
149 public boolean check() throws CheckException, InternalCheckException {
150 Enumeration iterator = almacen.elements();
151 SimpleCheck constraint = null;
152 this.match = true;
153 this.hasBeenChecked = true;
154 while (iterator.hasMoreElements()) {
155 constraint = (SimpleCheck) iterator.nextElement();
156 if (constraint.isChecked()) {
157 if (!constraint.match()) {
158 this.match = false;
159 }
160 } else {
161 if (!constraint.check()) {
162 this.match = false;
163 }
164 }
165 }
166 return this.match;
167 }
168
169 /***
170 * This method set the ValidConstrinatProcessor and executes it.
171 * @param constraintListener the validConstraintProcesor object
172 * to handle the process
173 * @return the String with the result of the execution
174 * @throws OkDoInternalException for internal error only
175 **/
176 public String executeOkDo(OkDo constraintListener)
177 throws OkDoInternalException {
178 this.setOkDo(constraintListener);
179 return this.validConstraintProcesor.execute();
180 }
181
182 /***
183 * This method returns a container with instances of
184 * invalidConstraintProcessors, one for each constraint that dosen´t
185 * match. It also adds the constainer´s parameters <br>
186 * <br>[Spanish] El funcionamiento es obtener todas las restricciones
187 * y comprobar si se han cumplido o no (se supone que todas están
188 * ejecutadas)
189 * Si no se han cumplido, entonces debemos crear un objeto para contener
190 * los parametros y pedir a la restricción que nos
191 * los proporcione Se añaden los parametros propios del contenedor de
192 * restricciones y se introducen en el contenedor
193 * de objetos de parametros.
194 * @return One instance of the InValidConstraintProcesorContainer
195 * indicated in the set method with all the
196 * errorDo
197 **/
198 public ErrorDoGroup getErrorDoGroup() {
199 Enumeration iterator = almacen.elements();
200 SimpleCheck constraint = null;
201 ErrorDo listener = null;
202 while (iterator.hasMoreElements()) {
203 constraint = (SimpleCheck) iterator.nextElement();
204 if (!(constraint).match()) {
205 //una nueva instancia del procesador de invalidez de restriccion
206 listener = constraintListener.getNewInstance();
207 //le pido a la restricción que me añada los parametros
208 constraint.errorDo(listener);
209 //le añado los parametros del contenedor
210 this.addDataParameters(listener);
211 //añado el procesador de invalidez al contenedor de
212 //procesadores de invalidez
213 constraintListenerContainer.addErrorDo(listener);
214 }
215 }
216 return this.constraintListenerContainer;
217 }
218
219 /***
220 * Returns the ValidConstraintProcessor
221 * @return the OkDo
222 **/
223 public OkDo getOkDo() {
224 return this.validConstraintProcesor;
225 }
226
227 /***
228 * Return the data to check <br>
229 * <br>[Spanish] Devuelve el valor a comprobar,
230 * este método es llamado por la restricción
231 * para saber que valor debe comprobar.
232 * @return the data to check
233 **/
234 public String getValueToCheck() {
235 return this.valueToCheck;
236 }
237
238 /***
239 * Return the name of the data to check <br>
240 * <br>[Spanish] Devuelve el nombre del valor a comprobar,
241 * por ejemplo el nombre del parametro del servlet.
242 * @return the name of the data to check
243 **/
244 public String getValueToCheckName() {
245 return this.valueToCheckName;
246 }
247
248 /***
249 * Return if this data has been checked
250 * @return true if this ConstraintCOntainer has been checked
251 **/
252 public boolean isChecked() {
253 return this.hasBeenChecked;
254 }
255
256 /***
257 * Return if throw an Exception if this constraintContainer
258 * dose´nt match some constraint
259 * @return true if throw or false if not
260 **/
261 public boolean isTrownException() {
262 return this.throwException;
263 }
264
265 /***
266 * Return if this data match or not (the value return by checkConstraints)
267 * @return the value of checkConstraint()
268 **/
269 public boolean match() {
270 return this.match;
271 }
272
273 /***
274 * This method is used by the consstraint to indicate to the container
275 * that needs one diferent constraint executed, if the
276 * container hasn´t then return false else returns true. If the
277 * constraint isn´t executed then the container executes it.
278 * <br>
279 * <br>[Spanish] Compruebo todas las restricciones que tengo a ver si
280 * alguna coincide con la que me piden. Si la
281 * tengo y no está ejecutada, la ejecuto. Si no la tengo, devuelvo false
282 * @param constraintName the class name of the constraint that needs
283 * (com.inigoserrano.isvalidator.checks.NotBlankCheck, for example)
284 * @return true if have execute else false
285 * @throws CheckException Throw if the data dosn´t match and the flag is on
286 * @throws InternalCheckException For internal error only
287 **/
288 public boolean needExecutedCheck(String constraintName)
289 throws CheckException, InternalCheckException {
290 //empiezo a buscarla
291 SimpleCheck constraint = null;
292 Enumeration iterator = almacen.elements();
293 while (iterator.hasMoreElements()) {
294 constraint = (SimpleCheck) iterator.nextElement();
295 if (constraintName.equals(constraint.getClass().getName())) {
296 if (constraint.isChecked()) {
297 return true;
298 } else {
299 constraint.check();
300 return true;
301 }
302 }
303 }
304 return false;
305 }
306
307 /***
308 * This method puts the InValidConstraintProcesor
309 * and InValidConstraintProcesorContainer
310 * @param constraintListener One instance of an InValidConstraintProcesor
311 * @param constraintListenerContainer One instance
312 * of an InValidConstraintProcesorContainer
313 **/
314 public void setErrorDo(
315 ErrorDo constraintListener,
316 ErrorDoGroup constraintListenerContainer) {
317 this.constraintListener = constraintListener;
318 this.constraintListenerContainer = constraintListenerContainer;
319 }
320
321 /***
322 * This method puts the OkDo
323 * @param constraintListener The OkDo
324 **/
325 public void setOkDo(OkDo constraintListener) {
326 this.validConstraintProcesor = constraintListener;
327 this.validConstraintProcesor.setData(this);
328 }
329 }
This page was automatically generated by Maven